import java.sql.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
//chaxun
        m1();

        m2();

        m3();

        m4();

        m5();







    }

    private static void m5() {

        try {
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入你想要修改的id");
            int id=sc.nextInt();

            Class.forName("com.mysql.cj.jdbc.Driver");

            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            Connection con = DriverManager.getConnection(url, "root", "111111");

            String sql_select="select * from user where id=?";
            PreparedStatement prepared = con.prepareStatement(sql_select);
            prepared.setObject(1,id);

            ResultSet set = prepared.executeQuery();

            if(set.next()){
                System.out.println("该id存在，请输入想要修改的账号");
                String name=sc.next();
                System.out.println("请输入想要修改的密码");
                String pass=sc.next();
                String sql="update user set name=?,pass=? where id=?";

                PreparedStatement preparedStatement = con.prepareStatement(sql);
                preparedStatement.setObject(1,name);
                preparedStatement.setObject(2,pass);
                preparedStatement.setObject(3,id);

                int i = preparedStatement.executeUpdate();

                if(i!=0){
                    System.out.println("更新成功");
                }
                preparedStatement.close();
            }else{
                System.out.println("id不存在");
            }

            prepared.close();
            con.close();

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    private static void m4() { try {

        Class.forName("com.mysql.cj.jdbc.Driver");

        String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
        Connection con = DriverManager.getConnection(url, "root", "111111");

        String sql="delete from user where id=?";

        PreparedStatement pst = con.prepareStatement(sql);

        pst.setObject(1,"2");

        int result=pst.executeUpdate();

        if(result>0){
            System.out.println("删除成功");
        }

        pst.close();
        con.close();

    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
    }


    private static void m3() {
        try {

            Scanner sc=new Scanner(System.in);
            System.out.println("请输入你要新增的姓名：");
            String name=sc.next();
            System.out.println("请输入你要新增的密码：");
            String pass=sc.next();

            Class.forName("com.mysql.cj.jdbc.Driver");

            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            Connection con = DriverManager.getConnection(url, "root", "111111");

            String sql="insert into user(name,pass) values(?,?)";

            PreparedStatement pst = con.prepareStatement(sql);

            pst.setObject(1,name);
            pst.setObject(2,pass);

            int result=pst.executeUpdate();

            if(result>0){
                System.out.println("新增成功");
            }

            pst.close();
            con.close();

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


    }


    private static void m2() {
        try {
            Scanner sc=new Scanner(System.in);
            System.out.println("1");
            String word=sc.next();

            Class.forName("com.mysql.cj.jdbc.Driver");
            String url ="jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            Connection connection= DriverManager.getConnection(url,"root","111111");
            // if(connection !=null){
            //    System.out.println("连接成功");
            //  }
            String sql ="select * from user where  name ?";

            PreparedStatement preparedStatement = connection.prepareStatement(sql);

preparedStatement.setObject(1,"%"+word+"%");

            ResultSet resultSet = preparedStatement.executeQuery();


            while ( resultSet.next()){
                int id = resultSet.getInt(1);
                int name = resultSet.getInt(2);
                int pass = resultSet.getInt(3);
                System.out.println(id+"-------"+name+"------"+pass);

            }

            resultSet.close();
            preparedStatement.close();
            connection.close();

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    private static void m1() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url ="jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            Connection connection= DriverManager.getConnection(url,"root","111111");
            // if(connection !=null){
            //    System.out.println("连接成功");
            //  }
            String sql ="select * from user";

            PreparedStatement preparedStatement = connection.prepareStatement(sql);

            ResultSet resultSet = preparedStatement.executeQuery();


            while ( resultSet.next()){
                int id = resultSet.getInt(1);
                int name = resultSet.getInt(2);
                int pass = resultSet.getInt(3);
                System.out.println(id+"-------"+name+"------"+pass);

            }

            resultSet.close();
            preparedStatement.close();
            connection.close();

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}